<?php

/**
 * 自动生成 MerWxMenuService
 * 
 * Apps/Common/Service/MerWxMenuService.class.php
 * Zix @ 2015-06-16
 */

namespace Common\Service;

use Common\Service\WechatSdk;

class MerWxMenuService {

  private static $instance;
  var $type_2_value = array(
    'click' => 'key',
    'view' => 'url',
    'scancode_push' => '',
    'scancode_waitmsg' => '',
    'pic_sysphoto' => '',
    'pic_photo_or_album' => '',
    'pic_weixin' => '',
    'location_select' => '',
  );

  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerWxMenuService();
    }

    return $instance;
  }

  function get_default_row() {
    return array();
  }

  function get_by_id($id) {
    $MerWxSetting = M('MerWxSetting');
    $data = $MerWxSetting->find($id);
    if (isset($data['wx_menu']) && !empty($data['wx_menu'])) {
      return $data['wx_menu'];
    } else {
      return array('button' => array());
    }
  }

  function save_menu($wx_id, $data) {
    $MerWxSetting = M('MerWxSetting');

    $ret = $MerWxSetting->where('id = %d', $wx_id)->save(array(
      'wx_menu' => json_encode($data, JSON_UNESCAPED_UNICODE)
    ));

    if ($ret) {
      return ajax_arr('保存成功', TRUE);
    } else {
      return ajax_arr('保存失败');
    }
  }

  //解析微信菜单数据
  function wx_data_2_self_data($wx_data) {
    $button = array();
    foreach ( $wx_data['menu']['button'] as $row ) {
      
      $tmp = array();
      $tmp['name'] = $row['name'];
      $tmp['value'] = '';
      if (isset($row['key']) || isset( $row['url'])) {
        $tmp['value'] = $row[$this->type_2_value[$row['type']]];
        $tmp['sub_button'] = array();
      } else if (isset($row['sub_button']) && !empty($row['sub_button'])) {
        $t = array();
        foreach ($row['sub_button'] as $r) {
          $t['name'] = $r['name'];
          $t['value'] = $r[$this->type_2_value[$r['type']]];
          $tmp['sub_button'][] = $t;
        }
      }
      $button[] = $tmp;
    }

    return array(
      'button' => $button
    );
  }

  //解析微信菜单数据
  function self_data_2_wx_data($self_data) {
    $post_data = array();
    foreach ($self_data['button'] as $key => $val) {
      $post_data[$key]['name'] = $val['name'];
      if (isset($val['sub_button']) && count($val['sub_button']) > 0) {
        foreach ($val['sub_button'] as $k => $v) {
          $post_data[$key]['sub_button'][$k]['name'] = $v['name'];
          if (is_url($v['value'])) {
            $post_data[$key]['sub_button'][$k]['type'] = 'view';
            $post_data[$key]['sub_button'][$k]['url'] = $v['value'];
          } else {
            $post_data[$key]['sub_button'][$k]['type'] = 'click';
            $post_data[$key]['sub_button'][$k]['key'] = $v['value'];
          }
        }
      } else {
        if (is_url($val['value'])) {
          $post_data[$key]['type'] = 'view';
          $post_data[$key]['url'] = $val['value'];
          $post_data[$key]['sub_button'] = array();
        } else {
          $post_data[$key]['type'] = 'click';
          $post_data[$key]['key'] = $val['value'];
        }
      }
    }
    return array(
      'button' => $post_data
    );
  }

}
